package com.frank.yinyuetai;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.SparseArray;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.frank.yinyuetai.Fragment.HomeFragment;
import com.frank.yinyuetai.Fragment.MvFragment;
import com.frank.yinyuetai.Fragment.VChartFragment;
import com.frank.yinyuetai.Fragment.YueDanFragment;
import com.lzy.okhttputils.OkHttpUtils;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.roughike.bottombar.BottomBar;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    @Bind(R.id.iv_setting)
    ImageView setting;
    @Bind(R.id.toolbar_main)
    Toolbar toolbar;
    @Bind(R.id.iv_home)
    ImageView homeBtn;
    @Bind(R.id.iv_mv)
    ImageView mvBtn;
    @Bind(R.id.iv_vbang)
    ImageView vbangBtn;
    @Bind(R.id.iv_yuedan)
    ImageView yuedanBtn;
    private SparseArray<Fragment> fragments;
    private BottomBar bottomBar;
    private FragmentManager fm;
    private HomeFragment fragment1;
    private MvFragment fragment2;
    private VChartFragment fragment3;
    private YueDanFragment fragment4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        OkHttpUtils.init(getApplication());
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        fragments = new SparseArray<>();
        fragments.put(R.id.bottomBarItem1, new HomeFragment());
        fragments.put(R.id.bottomBarItem2, new MvFragment());
        fragments.put(R.id.bottomBarItem3, new VChartFragment());
        fragments.put(R.id.bottomBarItem4, new YueDanFragment());
        homeBtn.setOnClickListener(this);
        mvBtn.setOnClickListener(this);
        vbangBtn.setOnClickListener(this);
        yuedanBtn.setOnClickListener(this);
        // setFragment(new HomeFragment());
        fm = getSupportFragmentManager();
        showFragment(1);
        homeBtn.setBackgroundColor(getResources().getColor(R.color.bb_darkBackgroundColor));

      /*  bottomBar = BottomBar.attach(this, savedInstanceState);
        bottomBar.setItemsFromMenu(R.menu.bottom_menu, new OnMenuTabClickListener() {
            @Override
            public void onMenuTabSelected(@IdRes int menuItemId) {
                Fragment targetFragment = fragments.get(menuItemId);
                setFragment(targetFragment);
            }

            @Override
            public void onMenuTabReSelected(@IdRes int menuItemId) {

            }
        });
        bottomBar.mapColorForTab(0, ContextCompat.getColor(this, R.color.colorPrimary));
        bottomBar.mapColorForTab(1, ContextCompat.getColor(this, R.color.colorPrimary));
        bottomBar.mapColorForTab(2, ContextCompat.getColor(this, R.color.colorPrimary));
        bottomBar.mapColorForTab(3, ContextCompat.getColor(this, R.color.colorPrimary));*/
        setTranslucentStatus(ContextCompat.getColor(this, R.color.colorPrimaryDark));
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SettingActivity.class);
                startActivity(intent);
            }
        });

    }

    private void setFragment(Fragment fragment) {
        if (fragment == null) {
            return;
        }
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (fragment.isAdded() && fragment.isVisible()) {
            return;
        }
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.replace(R.id.fl_content, fragment);
        }
        transaction.commit();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //保存底部导航状态 这一步是必须的
        //bottomBar.onSaveInstanceState(outState);
    }

    protected void setTranslucentStatus(int color) {
        //4.4-5.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setNavigationBarTintEnabled(true);
            tintManager.setTintColor(color);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_home:
                // setFragment(new HomeFragment());
                showFragment(1);
                homeBtn.setBackgroundColor(getResources().getColor(R.color.bb_darkBackgroundColor));
                mvBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
                vbangBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
                yuedanBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
                break;
            case R.id.iv_mv:
                //setFragment(new MvFragment());
                showFragment(2);
                mvBtn.setBackgroundColor(getResources().getColor(R.color.bb_darkBackgroundColor));
                homeBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
                yuedanBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
                vbangBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
                break;
            case R.id.iv_vbang:
                // setFragment(new VChartFragment());
                showFragment(3);
                vbangBtn.setBackgroundColor(getResources().getColor(R.color.bb_darkBackgroundColor));
                mvBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
                homeBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
                yuedanBtn.setBackgroundColor(getResources().getColor(R.color.transparent));

                break;
            case R.id.iv_yuedan:
                // setFragment(new YueDanFragment());
                showFragment(4);
                yuedanBtn.setBackgroundColor(getResources().getColor(R.color.bb_darkBackgroundColor));
                homeBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
                mvBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
                vbangBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
                break;
        }
    }

    public void showFragment(int index) {
        FragmentTransaction ft = fm.beginTransaction();

        // 想要显示一个fragment,先隐藏所有fragment，防止重叠
        hideFragments(ft);

        switch (index) {
            case 1:
                // 如果fragment1已经存在则将其显示出来
                if (fragment1 != null)
                    ft.show(fragment1);
                    // 否则是第一次切换则添加fragment1，注意添加后是会显示出来的，replace方法也是先remove后add
                else {
                    fragment1 = new HomeFragment();
                    ft.add(R.id.fl_content, fragment1);
                }
                break;
            case 2:
                if (fragment2 != null)
                    ft.show(fragment2);
                else {
                    fragment2 = new MvFragment();
                    ft.add(R.id.fl_content, fragment2);
                }
                break;
            case 3:
                if (fragment3 != null)
                    ft.show(fragment3);
                else {
                    fragment3 = new VChartFragment();
                    ft.add(R.id.fl_content, fragment3);
                }
                break;
            case 4:
                if (fragment4 != null)
                    ft.show(fragment4);
                else {
                    fragment4 = new YueDanFragment();
                    ft.add(R.id.fl_content, fragment4);
                }
                break;
        }
        ft.commit();
    }

    // 当fragment已被实例化，就隐藏起来
    public void hideFragments(FragmentTransaction ft) {
        if (fragment1 != null)
            ft.hide(fragment1);
        if (fragment2 != null)
            ft.hide(fragment2);
        if (fragment3 != null)
            ft.hide(fragment3);
        if (fragment4 != null)
            ft.hide(fragment4);
    }
}
